﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;
using WIAssistant;

namespace Test_WIAssistant
{
    /// <summary>
    /// Summary description for Test_TFSConnections
    /// </summary>
    [TestClass]
    public class Test_TFSConnections
    {
        public Test_TFSConnections()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private MainMediator  _mediator;
        private IMain _form;

        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            _form = MockRepository.GenerateStub<IMain>();
            _mediator = new MainMediator(_form);
        }

        [TestMethod]
        public void TestGetTFSServers()
        {
            // Arrange
            

            // Act
            _mediator.FillServerList();
            

            // Assert
            Assert.IsTrue(_mediator.GetTFSServerNames().Count > 0);

        }

        [TestMethod]
        public void TestConnectToTFSServer()
        {
            //Arrange
            string server;
            //Act
            _mediator.FillServerList();
            _form.CurrentServer = _mediator.GetTFSServerNames().First();
            _mediator.ServerSelected();

            //Assert
            Assert.IsTrue(_mediator.ConnectedToTFS);
        }

        [TestMethod]
        public void TestClearAll()
        {
            // Arrange
            string server;
            List<ProjectInfo> projects;

            // Act
            _mediator.FillServerList();
            _form.CurrentServer = _mediator.GetTFSServerNames().First();
            _mediator.ServerSelected();
            _form.CurrentProject = _mediator.GetTFSprojects().Where(x => x.Name == "ESP-Tester").Single();
            _mediator.ClearAll();

            //Assert
            Assert.IsFalse(_mediator.ConnectedToTFS);
        }

        [TestMethod]
        public void TestClearProject()
        {
            // Arrange
            string server;
            List<ProjectInfo> projects;

            // Act
            _mediator.FillServerList();
            _form.CurrentServer = _mediator.GetTFSServerNames().First();
            _mediator.ServerSelected();
            _form.CurrentProject = _mediator.GetTFSprojects().Where(x => x.Name == "ESP-Tester").Single();
            _mediator.ClearProject();

            //Assert
            Assert.IsTrue(_form.CurrentProject == null);
        }

        [TestMethod]
        public void TestGetQueryList()
        {
            //Arrange
            
            //Act
            _mediator.FillServerList();
            _form.CurrentServer = _mediator.GetTFSServerNames().First();
            _mediator.ServerSelected();
            _form.CurrentProject = _mediator.GetTFSprojects().Where(x => x.Name == "ESP-Tester").Single();
            _mediator.ProjectSelected();

            //Assert
            Assert.IsTrue(_mediator.TeamStoredQueryNames.Count > 0);
        }


        [TestMethod]
        public void TestRunQuery()
        {
            //Arrange
            
            //Act
            _mediator.FillServerList();
            _form.CurrentServer = _mediator.GetTFSServerNames().First();
            _mediator.ServerSelected();
            _form.CurrentProject = _mediator.GetTFSprojects().Where(x => x.Name == "ESP-Tester").Single();
            _mediator.ProjectSelected();
            _form.CurrentQuery = _mediator.TeamStoredQueryNames.First();
            _mediator.QuerySelected();
            

            //Assert
        }


        [TestMethod]
        public void TestAddItemToPad()
        {
            //Arrange
            _mediator.FillServerList();
            _form.CurrentServer = _mediator.GetTFSServerNames().First();
            _mediator.ServerSelected();
            Project currentProject = GetTestingProject(_mediator.GetTFSprojects(), _mediator.GetProjectFromInfo);
            var workItem = new WorkItem(currentProject.WorkItemTypes["Bug"]);

            //Act
            _mediator.AddWorkItemToPad(workItem);

            
            //Assert
            Assert.IsTrue(_mediator.Pad.Count > 0);
        }

        [TestMethod]
        public void TestRemoveItemFromPad()
        {
            //Arrange
            _mediator.FillServerList();
            _form.CurrentServer = _mediator.GetTFSServerNames().First();
            _mediator.ServerSelected();
            Project currentProject = GetTestingProject(_mediator.GetTFSprojects(), _mediator.GetProjectFromInfo);
            var workItem = new WorkItem(currentProject.WorkItemTypes["Bug"]);
            _mediator.AddWorkItemToPad(workItem);

            //Act
            _mediator.RemoveWorkItemFromPad(workItem);

            //Assert
            Assert.IsTrue(_mediator.Pad.Count == 0);
        }


        [TestMethod]
        public void TestCreateLink()
        {
            //Arrange
            _mediator.FillServerList();
            _form.CurrentServer = _mediator.GetTFSServerNames().First();
            _mediator.ServerSelected();
            _form.CurrentProject = _mediator.GetTFSprojects().Where(x => x.Name == "ESP-Tester").Single();
            _mediator.ProjectSelected();
            _form.CurrentQuery = _mediator.TeamStoredQueryNames.Where(x => x == "All Bugs").Single();
            _mediator.QuerySelected();

            // Don't save this!!!
            WorkItem linkPickedHalf = _mediator.QueryResults[0];
            _mediator.PickedWorkItem = linkPickedHalf;
            
            var padItem = _mediator.QueryResults[1];;

            _mediator.Pad.Add(padItem, null);
             
            
            //Act
            _mediator.LinkPickedAndPad(false, null, "Unit Test Link Comment - Should never be seen");

            //Assert
            Assert.IsTrue(_mediator.PickedWorkItem.Links.Count > 0);
        }

       

        private static Project GetTestingProject(IEnumerable<ProjectInfo> projectInfos, Func<ProjectInfo, Project> convertToProjectFunc)
        {
            ProjectInfo projectInfo = projectInfos.Where(x => x.Name == "ESP-Tester").Single();
            return convertToProjectFunc(projectInfo);
        }
    }
}
